package 队列与栈;

import java.util.Stack;

//有效括号
//https://leetcode.cn/problems/valid-parentheses/
class 有效括号 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else if (c == ')') {
                if (stack.isEmpty()) {
                    return false;
                }
                Character left = stack.pop();
                if (left != '(') {
                    return false;
                }
            } else if (c == '}') {
                if (stack.isEmpty()) {
                    return false;
                }
                Character left = stack.pop();
                if (left != '{') {
                    return false;
                }
            } else if (c == ']') {
                if (stack.isEmpty()) {
                    return false;
                }
                Character left = stack.pop();
                if (left != '[') {
                    return false;
                }
            }
        }
        return stack.empty();
    }
}
